package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.EmployeeVerification;
import com.amsp.oa.oademo.service.IEmployeeVerificationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/employee-verification")
@Api(tags = "员工入职信息核定表管理")
@CrossOrigin
public class EmployeeVerificationController {

    @Autowired
    private IEmployeeVerificationService employeeVerificationService;

    @GetMapping("/list")
    @ApiOperation("获取员工入职信息核定表列表")
    public ResponseEntity<Map<String, Object>> list(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer limit,
            @ApiParam("部门") @RequestParam(required = false) String department,
            @ApiParam("岗位") @RequestParam(required = false) String position) {

        Page<EmployeeVerification> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<EmployeeVerification> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.hasText(department)) {
            wrapper.like(EmployeeVerification::getDepartment, department);
        }
        if (StringUtils.hasText(position)) {
            wrapper.like(EmployeeVerification::getPosition, position);
        }

        wrapper.orderByDesc(EmployeeVerification::getId);

        Page<EmployeeVerification> pageResult = employeeVerificationService.page(pageParam, wrapper);

        Map<String, Object> result = new HashMap<>();
        result.put("total", pageResult.getTotal());
        result.put("data", pageResult.getRecords());

        return ResponseEntity.ok(result);
    }

    @PostMapping
    @ApiOperation("创建员工入职信息核定表")
    public ResponseEntity<EmployeeVerification> create(@RequestBody EmployeeVerification employeeVerification) {
        employeeVerificationService.save(employeeVerification);
        return ResponseEntity.ok(employeeVerification);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新员工入职信息核定表")
    public ResponseEntity<EmployeeVerification> update(
            @PathVariable Long id,
            @RequestBody EmployeeVerification employeeVerification) {
        employeeVerification.setId(id);
        employeeVerificationService.updateById(employeeVerification);
        return ResponseEntity.ok(employeeVerification);
    }

    @GetMapping("/{id}")
    @ApiOperation("获取员工入职信息核定表详情")
    public ResponseEntity<EmployeeVerification> getById(@PathVariable Long id) {
        return ResponseEntity.ok(employeeVerificationService.getById(id));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除员工入职信息核定表")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        employeeVerificationService.removeById(id);
        return ResponseEntity.ok().build();
    }
}